/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.florent37.github.com.expansionpanel.slice;

import com.florent37.github.com.expansionpanel.ExpansionHeader;
import com.florent37.github.com.expansionpanel.ExpansionLayout;
import com.florent37.github.com.expansionpanel.ResourceTable;
import com.florent37.github.com.expansionpanel.viewgroup.ExpansionLayoutCollection;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class ExpansionPanelSampleAbilityProgrammaticallySlice extends AbilitySlice {

    ComponentContainer mDynamicLayoutContainer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_expansion_panel_sample_programmatically);
        mDynamicLayoutContainer = (DirectionalLayout) findComponentById(ResourceTable.Id_dynamicLayoutContainer);
        final ExpansionLayout ex1 = addDynamicLayout();
        final ExpansionLayout ex2 = addDynamicLayout();
        final ExpansionLayoutCollection expansionLayoutCollection = new ExpansionLayoutCollection();
        expansionLayoutCollection.add(ex1).add(ex2);
        expansionLayoutCollection.openOnlyOne(true);
    }

    private ExpansionLayout addDynamicLayout() {
        final ExpansionHeader expansionHeader = createExpansionHeader();
        mDynamicLayoutContainer.addComponent(expansionHeader, ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        final ExpansionLayout expansionLayout = createExpansionLayout();
        mDynamicLayoutContainer.addComponent(expansionLayout, ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        expansionHeader.setExpansionLayout(expansionLayout);
        return expansionLayout;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private ExpansionLayout createExpansionLayout() {
        final ExpansionLayout expansionLayout = new ExpansionLayout(this);
        DirectionalLayout layout = new DirectionalLayout(this);
        layout.setOrientation(DirectionalLayout.VERTICAL);
        expansionLayout.addComponent(layout, ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        final Text text = new Text(this);
        text.setText("Content");
        text.setTextSize(42);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setTextColor(new Color(Color.getIntColor("#3E3E3E")));
        ShapeElement backgroundElement = new ShapeElement();
        backgroundElement.setRgbColor(new RgbColor(235, 235, 235));
        layout.setBackground(backgroundElement);
        layout.addComponent(text, ComponentContainer.LayoutConfig.MATCH_PARENT, 600);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                System.out.println("wtf add view");
                final Component child = new Component(ExpansionPanelSampleAbilityProgrammaticallySlice.this);
                ShapeElement backgroundElement = new ShapeElement();
                backgroundElement.setRgbColor(new RgbColor(000, 000, 000));
                child.setBackground(backgroundElement);
                layout.addComponent(child, ComponentContainer.LayoutConfig.MATCH_PARENT, 100);
            }
        });
        ComponentContainer parse = (ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_expansion_panel_sample_panel, layout, false);
        ((ExpansionHeader) parse.getComponentAt(0)).init(ResourceTable.Id_expansionLayout, true, ResourceTable.Id_headerIndicator, 0, 0);
        layout.addComponent(parse);
        return expansionLayout;
    }

    private ExpansionHeader createExpansionHeader() {
        final ExpansionHeader expansionHeader = new ExpansionHeader(this);
        ShapeElement backgroundElement = new ShapeElement();
        backgroundElement.setRgbColor(new RgbColor(0xff, 0xff, 0xff));
        expansionHeader.setBackground(backgroundElement);
        final DependentLayout layout = new DependentLayout(this);
        expansionHeader.addComponent(layout, ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        layout.setPadding(48, 28, 62, 28);
        final Image expansionIndicator = new Image(this);
        expansionIndicator.setPixelMap(ResourceTable.Media_ic_expansion_header_indicator_grey_24dp);
        DependentLayout.LayoutConfig imageLayoutParams = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        imageLayoutParams.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_RIGHT);
        imageLayoutParams.addRule(DependentLayout.LayoutConfig.VERTICAL_CENTER);
        expansionIndicator.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
        layout.addComponent(expansionIndicator, imageLayoutParams);
        expansionIndicator.setHeight(36);
        expansionIndicator.setWidth(36);
        Text text = new Text(this);
        text.setText("Trip name");
        text.setTextSize(42);
        text.setTextColor(new Color(Color.getIntColor("#3E3E3E")));
        DependentLayout.LayoutConfig textLayoutParams = new DependentLayout.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        textLayoutParams.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_LEFT);
        textLayoutParams.addRule(DependentLayout.LayoutConfig.VERTICAL_CENTER);
        layout.addComponent(text, textLayoutParams);
        expansionHeader.setExpansionHeaderIndicator(expansionIndicator);
        return expansionHeader;
    }
}
